<include file="Public:header" />
<include file="nav" />
<form id="myForm" method="post" action="{:U('export')}">
<div class="listbox">
<table width="100%" border="0" cellspacing="1" cellpadding="0">
        <thead>
            <tr>
                <th width="48"><input checked="checked" type="checkbox" id="ChkAll" onClick="CheckAll(this.form)">选择</th>
                <th>表名</th>
                <th width="120">数据量</th>
                <th width="120">数据大小</th>
                <th width="160">创建时间</th>
                <th width="160">备份状态</th>
                <th width="120">操作</th>
            </tr>
        </thead>
        <tbody>
            <volist name="list" id="table">
                <tr>
                    <td class="num" align="center">
                        <input checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                    </td>
                    <td>{$table.name}</td>
                    <td align="center">{$table.rows}</td>
                    <td align="center">{$table.data_length|formatBytes}</td>
                    <td>{$table.create_time}</td>
                    <td class="info" align="center">未备份</td>
                    <td class="action">
                        <a href="{:U('optimize?tables='.$table['name'])}">优化表</a>&nbsp;|&nbsp;
                        <a href="{:U('repair?tables='.$table['name'])}">修复表</a>
                    </td>
                </tr>
            </volist>
        </tbody>
        <tfoot>
        <tr>
            <td colspan="200">&nbsp;<input class="btn" id="optimize" type="button" value="优化表" href="{:U('optimize')}" />&nbsp;<input class="btn" id="repair" href="{:U('repair')}" type="button" value="修复表" />&nbsp;<input class="btn" id="export" type="button" value="立即备份" /></td>
        </tr>
        </tfoot>
    </table>
</div>
</form>

<script type="text/javascript">
    (function($){
        var $form = $("#myForm"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post($(this).attr("href"), $form.serialize(), function(data){
                if(data.status){
                    alert(data.info);
                } else {
                    alert(data.info);
                }
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.val("发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status){
                        tables = data.tables;
                        $export.val(data.info + "正在备份,请勿关闭！");
                        backup(data.tab);
                        window.onbeforeunload = function(){ return "正在备份,请勿关闭！" }
                    } else {
                        alert(data.info);
                        $export.val("立即备份");
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);

                    if(!$.isPlainObject(data.tab)){
                        $export.val("备份完成");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    alert(data.info);
                    $export.html("立即备份");
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
    </script>
<include file="Public:footer" />